package com.example.lanou.mynews.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.lanou.mynews.R;
import com.example.lanou.mynews.adapter.GridViewAdapter;
import com.example.lanou.mynews.adapter.NewsFragAdapter;
import com.example.lanou.mynews.application.MyApplication;
import com.example.lanou.mynews.beans.AllTitles;
import com.example.lanou.mynews.db.dao.Titles;
import com.example.lanou.mynews.single.SingleDao;
import com.example.lanou.mynews.single.SingleMode;

import java.util.ArrayList;


/**
 * Created by dllo on 16/1/8.
 */
public class MainTabNews extends Fragment {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private NewsFragAdapter adapter;
    private RecyclerView gridViewOn;
    private RecyclerView gridViedOff;
    private GridViewAdapter adapterOn;
    private GridViewAdapter adapterOff;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_frag_news, container, false);
        setHasOptionsMenu(true);

        view.findViewById(R.id.news_frag_tab_down_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showAllTitleDia();
            }
        });
        tabLayout = (TabLayout) view.findViewById(R.id.news_frag_tablayout);
        viewPager = (ViewPager) view.findViewById(R.id.news_frag_vp);
        adapter = new NewsFragAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        return view;
    }


    private void showAllTitleDia() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        View view = LayoutInflater.from(getActivity()).inflate(R.layout.main_frag_title_btnall, null);

        gridViewOn = (RecyclerView) view.findViewById(R.id.main_frag_btnall_gridview);
        gridViedOff = (RecyclerView) view.findViewById(R.id.main_all_down_gridview);
        gridViewOn.setLayoutManager(new GridLayoutManager(MyApplication.getContext(), 4));
        gridViedOff.setLayoutManager(new GridLayoutManager(MyApplication.getContext(), 4));
        ArrayList<String> data = new ArrayList<>();
        for (Titles titles1 : SingleDao.getInstance().getLikeTitle()) {
            data.add(titles1.getName());
        }
        adapterOn = new GridViewAdapter(data);
        ArrayList<String> dataOff = new ArrayList<>();
        for (Titles titles : SingleDao.getInstance().getNotLike()) {
            dataOff.add(titles.getName());
        }
        adapterOff = new GridViewAdapter(dataOff);
        gridViewOn.setAdapter(adapterOn);
        gridViedOff.setAdapter(adapterOff);

        builder.setView(view);


        builder.show();


    }


}
